package com.shameit.goodqrinfomanager.ui.activity.login;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.shameit.goodqrinfomanager.AppViewModelFactory;
import com.shameit.goodqrinfomanager.BR;
import com.shameit.goodqrinfomanager.BaseMVVMActivity;
import com.shameit.goodqrinfomanager.MyApplication;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.bean.litepal.LoginUser;
import com.shameit.goodqrinfomanager.databinding.ActivityLoginBinding;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {
    private static long mExitTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        subscribeUi();
        //setToolBarBack(binding.toolbar);
    }

    @Override
    protected String setTitle() {
        return "登录";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void loadData() {

    }

    private void subscribeUi() {
        viewModel.loginUserMutableLiveData.observe(this, new Observer<LoginUser>() {
            @Override
            public void onChanged(LoginUser loginUser) {
                MyApplication.login(loginUser);
            }
        });

    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.viewmodel;
    }

    @Override
    public LoginViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(LoginViewModel.class);
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            //Toast.makeText(this, getString(R.string.activity_main_exit_hint), Toast.LENGTH_SHORT).show();
            ToastUtils.showShort(getApplication().getString(R.string.activity_main_exit_hint));
            mExitTime = System.currentTimeMillis();
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
            System.exit(0);
        }
    }

}
